package com.autoEcole.service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.autoEcole.dao.LeconDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Lecon;

public class LeconService {
	
	private LeconDAO leconDAO;
	/**
	 * LeconService
	 */
	private static LeconService leconService;
	
	/**
	 * Constructor
	 */
	private LeconService() {
		super();
	}

	/**
	 * Returns the LeconService instance (Design Pattern : Singleton)
	 * 
	 * @return LeconService
	 */
	public static synchronized LeconService getInstance() {
		if (leconService == null)
			leconService = new LeconService();
		return leconService;
	}

	public LeconDAO getLeconDAO() {
		return leconDAO;
	}

	public void setLeconDAO(LeconDAO leconDAO) {
		this.leconDAO = leconDAO;
	}

	public int create(Lecon lecon) throws ApplicationException {
		try {
			return leconDAO.create(lecon);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(Lecon lecon) throws ApplicationException {
		try {
			leconDAO.update(lecon);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Lecon findById(int id) throws ApplicationException {
		try {
			return leconDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * @throws ParseException 
	 * 
	 */
	public List<Lecon> findAll() throws ApplicationException, ParseException {
		try {
			return leconDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Lecon> findAllMap() throws ApplicationException {
		try {
			return leconDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int leconId) throws ApplicationException {
		try {
			leconDAO.delete(leconId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
}
